package com.iknight.theory.controller;

import com.iknight.mgr.core.template.ExcelParseTemplate;
import com.iknight.mgr.core.utils.ExcelUtils;
import com.iknight.mgr.core.utils.IkgWebUtils;
import com.iknight.theory.api.TheoryCommService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.HttpMethod;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@RestController
@RequestMapping("/rest/theory")
@Api("Excel解析控制器")
public class ExcelParseController {
    private static final Logger logger = LogManager.getLogger(ExcelParseController.class);
    @Autowired
    private ResourceLoader resourceLoader;

    @Autowired
    private TheoryCommService theoryCommService;

    @Value("${tgTemplate}")
    private String tgTemplateUri;

    @PostMapping("/parseExcel")
    @ApiOperation(value = "Excel解析方法", httpMethod = "POST")
    public Object parseExcel(@RequestParam("files") CommonsMultipartFile file, @RequestParam("code") String code, HttpServletRequest request, HttpServletResponse response) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        long start = Calendar.getInstance().getTimeInMillis();
        Resource resource = resourceLoader.getResource(tgTemplateUri);

        String msg = null;

        logger.info("开始读取定义的解析模板文件:" + sdf.format(Calendar.getInstance().getTime()));

        msg = "开始读取定义的解析模板文件.";
        IkgWebUtils.sendMsg(code, msg);

        JSONObject template = ExcelParseTemplate.getTgTemplate(resource);
        logger.info("结束解析定义的模板文件:" + sdf.format(Calendar.getInstance().getTime()));
        msg = "结束解析定义的模板文件.";
        IkgWebUtils.sendMsg(code, msg);

        JSONObject result;
        try {
            FileInputStream fis = (FileInputStream) file.getInputStream();
            logger.info("执行数据解析:" + sdf.format(Calendar.getInstance().getTime()));
            msg = "执行数据解析.";
            IkgWebUtils.sendMsg(code, msg);

            result = ExcelUtils.parseExcelFile(code, fis, template);//解析数据
            logger.info("结束数据解析:" + sdf.format(Calendar.getInstance().getTime()));

            msg = "结束数据解析.";
            IkgWebUtils.sendMsg(code, msg);

            //数据遍历
            List<JSONObject> resultList = (List<JSONObject>) result.get("result");
            JSONObject jsonObject = JSONObject.fromObject(theoryCommService.analyzeParsedExcel(resultList));

            msg = "执行数据插入结束.";
            IkgWebUtils.sendMsg(code, msg);
            IkgWebUtils.sendMsg(code, jsonObject.toString());

            long end = Calendar.getInstance().getTimeInMillis();
            long betweenDate = end - start;

            msg = "导入完成.用时 : " + betweenDate + "ms";
            IkgWebUtils.sendMsg(code, msg);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
